﻿using NHibernate;
using MiserDB.MySql;
using NHibernate.Criterion;
using System.Collections.Generic;
using System.IO;
using System;
using MiserBlogHelper.Helper;

namespace MiserDB.DataHelp
{
    public class QueryHelper : A_DataHelper
    {

        public virtual T GetData<T>(int id) where T : class
        {
            T re = Session.Get<T>(id);
            return re;
        }


        public virtual IList<T> GetList<T>(int fkId) 
        {
            return GetList<T>(fkId, null);
        }

        public virtual IList<T> GetList<T>(int fkId, HttpContextHelper contextHelp)
        {
            List<SimpleExpression> whereAry = new List<SimpleExpression>();
            whereAry.Add(Restrictions.Eq("Id", fkId));
            return GetList<T>(whereAry, contextHelp);
        }

        public virtual IList<T> GetList<T>(List<SimpleExpression> whereAry, HttpContextHelper contextHelp)
        {
            ICriteria list = Session.CreateCriteria(typeof(T));
            if (whereAry != null)
            {
                foreach (var item in whereAry)
                {
                    list = list.Add(item);
                }
            }
            if (contextHelp != null)
            {
                list = list.SetFirstResult(PageHelper.GetPageBegin(contextHelp)).SetMaxResults(contextHelp.PageSize);//分页
            }
            return list.List<T>();
        }

        public struct WhereKey
        {
            public string ItemName;
            public string ItemAlias;

            public WhereKey(string name, string alias)
            {
                this.ItemName = name;
                this.ItemAlias = alias;
            }
        }

        public virtual IList<T> GetList<T>(List<WhereKey> whereKey, List<SimpleExpression> whereAry, HttpContextHelper contextHelp)
        {
            ICriteria list = Session.CreateCriteria(typeof(T));

            if (whereKey != null)
            {
                foreach (var item in whereKey)
                {
                    list = list.CreateAlias(item.ItemName, item.ItemAlias);
                    
                }
            }

            if (whereAry != null)
            {
                foreach (var item in whereAry)
                {
                    list = list.Add(item);
                }
            }
            if (contextHelp != null)
            {
                list = list.SetFirstResult(PageHelper.GetPageBegin(contextHelp)).SetMaxResults(contextHelp.PageSize);//分页
            }
            return list.List<T>();
        }


        public virtual IList<T> GetSelfPage<T>(int fkId, HttpContextHelper contextHelp)
        {
            ICriteria list = Session.CreateCriteria(typeof(T))
                    .CreateCriteria("User")
                    .Add(Restrictions.Eq("Id", fkId));
            if (contextHelp != null)
            {
                list = list.SetFirstResult(PageHelper.GetPageBegin(contextHelp)).SetMaxResults(contextHelp.PageSize);//分页

            }

            return list.List<T>();
        }


        public virtual IList<T> GetListT<T>()
        {
            ICriteria list = Session.CreateCriteria(typeof(T));

            Session.CreateQuery("from Discussion where Id = GroupID");
            return list.List<T>();
        }
    }


}
